package com.thomas.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.thomas.entity.Blog;
import com.thomas.lucene.BlogIndex;
import com.thomas.service.BlogService;
import com.thomas.service.CommentService;
import com.thomas.util.StringUtil;

/** 
 * @ClassName: BlogController 
 * @Description: 博客Controller层
 * @author thomas_liu  
 * @date 2017年6月7日 下午11:55:05 
 */
@Controller
@RequestMapping("/blog")
public class BlogController {
	
	@Autowired
	private BlogService blogService;
	
	@Autowired
	private CommentService commentService;
	
	private BlogIndex blogIndex = new BlogIndex();
	
	/**
	 *请求博客详细信息 
	 *@param id
	 *@param request
	 *@return
	 *@throws Exception 
	 */
	@RequestMapping("/articles/{id}")
	public ModelAndView details(@PathVariable("id") Integer id,HttpServletRequest request){
		ModelAndView mav = new ModelAndView();
		Blog blog = blogService.findById(id);
		String keyWords = blog.getKeyWord();
		if(StringUtil.isNotEmpty(keyWords)){
			String arr[]=keyWords.split(" ");
			mav.addObject("keyWords", StringUtil.filterWhite(Arrays.asList(arr)));
		}else{
			mav.addObject("keyWords", null);
		}
		mav.addObject("blog", blog);
		blog.setClickHit(blog.getClickHit()+1);
		blogService.update(blog);
		
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("blogId", blog.getId());
		map.put("state", 1);
		mav.addObject("commentList", commentService.list(map));
		mav.addObject("pageCode", this.getUpAndDownPageCode(blogService.getLastBlog(id), blogService.getNextBlog(id), request.getServletContext().getContextPath()));
		mav.addObject("pageTitle", blog.getTitle()+"java开源博客系统");
		mav.addObject("mainPage", "foreground/blog/view.jsp");
		mav.setViewName("mainTemp");
		return mav;
	}
	
	/**
	 *获取上一篇博客和下一篇博客
	 *@param lastBlog 
	 *@param nextBlog
	 *@return  
	 */
	private String getUpAndDownPageCode(Blog lastBlog,Blog nextBlog,String projectContext){
		StringBuffer pageCode = new StringBuffer();
		if(lastBlog==null || lastBlog.getId()==null){
			pageCode.append("<p>上一篇: 没有了</p>");
		}else{
			pageCode.append("<p>上一篇： <a href='"+projectContext+"/blog/articles/"+lastBlog.getId()+".html'>"+lastBlog.getTitle()+"</a></p>");
		}
		
		if(nextBlog==null || nextBlog.getId()==null){
			pageCode.append("<p>下一篇: 没有了</p>");
		}else{
			pageCode.append("<p>下一篇： <a href='"+projectContext+"/blog/articles/"+nextBlog.getId()+".html'>"+nextBlog.getTitle()+"</a></p>");
		}
		
		return pageCode.toString();
	}
	
	/**
	 *获取上一页，下一页代码 查询博客用到 
	 *@param page 当前页
	 *@param totalNum 总记录数
	 *@param q 查询关键字
	 *@param pageSize 每页大小
	 *@param projectContext
	 *@return 
	 */
	private String getUpAndDownPageCode(Integer page,Integer totalNum,String q,Integer pageSize,String projectContext){
		long totalPage = totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1;
		StringBuffer pageCode = new StringBuffer();
		if(totalPage==0){
			return "";
		}else{
			pageCode.append("<nav>");
			pageCode.append("<ul class='pager'>");
			if(page>1){
				pageCode.append("<li><a href='"+projectContext+"/blog/q.html?page="+(page-1)+"&q="+q+"'>上一页</a></li>");
			}else{
				pageCode.append("<li class='disabled'><a href='#'>上一页</a></li>");
			}
			if(page<totalPage){
				pageCode.append("<li><a href='"+projectContext+"/blog/q.html?page="+(page+1)+"&q="+q+"'>下一页</a></li>");
			}else{
				pageCode.append("<li class='disabled'><a href='#'>下一页</a></li>");				
			}
			pageCode.append("</ul>");
			pageCode.append("</nav>");
		}
		
		return pageCode.toString();
	}
	
	/**
	 *根据关键字查询相关博客信息 
	 *@param q
	 *@return 
	 *@throws Exception 
	 */
	@RequestMapping("/q")
	public ModelAndView search(@RequestParam(value="q",required=false) String q,@RequestParam(value="page",required=false)String page,HttpServletRequest request)throws Exception{
		Integer pageSize = 3;
		if(StringUtil.isEmpty(page)){
			page="1";
		}
		ModelAndView mav = new ModelAndView();
		List<Blog> blogList = blogIndex.searchBlog(q.trim());
		Integer toIndex = blogList.size()>=Integer.parseInt(page)*pageSize?Integer.parseInt(page)*pageSize:blogList.size();
		mav.addObject("blogList", blogList.subList((Integer.parseInt(page)-1)*pageSize, toIndex));
		mav.addObject("pageCode", this.getUpAndDownPageCode(Integer.parseInt(page), blogList.size(), q, pageSize, request.getServletContext().getContextPath()));
		mav.addObject("q", q);
		mav.addObject("resultTotal", blogList.size());
		mav.addObject("pageTitle", "搜索关键字'"+q+"'结果页面_java开源博客系统");
		mav.addObject("mainPage","foreground/blog/result.jsp");
		mav.setViewName("mainTemp");
		return mav;
	}
	
}
